//
//  CZMainTabBar.swift
//  swiftsinaweibo
//
//  Created by 曾攀 on 16/3/20.
//  Copyright © 2016年 曾攀. All rights reserved.
//

import UIKit

// TabBarItem个数
let tabBarItemCount = 5

class CZMainTabBar: UITabBar {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //计算item的高度
        let width = frame.width / CGFloat(tabBarItemCount)
        // 计算最左边的这个frame,后面使用CGRectOffset
        let originFrame = CGRect(x: 0, y: 0, width: width, height: frame.height)
        var index = 0
        for view in subviews{
            // 判断是否是 UITabBarButton
            let cls = NSClassFromString("UITabBarButton")
            if view.isKindOfClass(cls!){
                // rect: 原始的frame
                // dx: x方向的偏移量
                // dy: y方向的偏移量
                view.frame = CGRectOffset(originFrame, CGFloat(index) * width, 0)
                index += index == 1 ? 2 : 1
                
            }
        }
        //设置按钮frame
        composeBtn.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.height)
    }
    
    
    
    //MARK:懒加载
    private lazy var composeBtn : UIButton = {
        let button = UIButton()

        //设置
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState:
        UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
    
         // 添加到tabBar上面
        self.addSubview(button)
        return button
        
    }()

}
